<template>
  <!--1.首先，弹窗页面中要有el-dialog组件即弹窗组件，我们把弹窗中的内容放在el-dialog组件中-->
  <!--2.设置:visible.sync属性，动态绑定一个布尔值，通过这个属性来控制弹窗是否弹出-->
  <el-dialog title="新增书本" :visible.sync="detailVisible" class="dialog">
    <div >
      <form>
        <div class="outline">
          <el-col class="inputImg-col"><!--上传书籍封面-->
            <el-row class="inputImg-gray"  ref="inputImg"  :style="{ 'background-image': `url(${filePath})`,'background-size': 'cover' }">
              <label for="upload" class="bookImg-font">上传书籍封面</label>
              <input id="upload" type="file" style="display: none;" @change="handleFileUpload">
            </el-row>
          </el-col>
          <div class="center">
            <div class="line">
              <label for="name" >书籍名称:</label>
              <input id="name" v-model="formMsg.name" type="text">
            </div>
            <div class="line">
              <label for="writer" >作者:</label>
              <input id="writer" v-model="formMsg.writer" type="text">
            </div>
            <div class="line">
              <label for="print" >出版社:</label>
              <input id="print" v-model="formMsg.publisher" type="text">
            </div>
            <div class="line">
              <label for="date" >出版日期:</label>
              <input id="date" v-model="formMsg.date" type="text">
            </div>
            <div class="line">
              <label for="code" >书籍条形码:</label>
              <input id="code" v-model="formMsg.code" type="text">
            </div>
            <div class="line">
              <label for="type" >书籍类型:</label>
              <input id="type" v-model="formMsg.type" type="text">
            </div>
            <div class="line">
              <label for="type" class="label-left" >库存:</label>
              <input id="type" v-model="formMsg.stock" type="text">
            </div>
            <div class="line">
              <label for="type" class="label-left">价格:</label>
              <input id="type" v-model="formMsg.price" type="text">
            </div>
          </div>
          <div class="right">
            <input type="button" value="新增" class="store" @click="onSubmit">
            <br>
            <input type="button" value="取消" class="cancel" @click="detailVisible=false">
          </div>
        </div>
      </form>
    </div>
  </el-dialog>
</template>

<script>
import axios from "axios";

export default {
  name: "dialogComponent",
  data() {
    return {
      detailVisible: false,
      filePath:"",
      fileName:"",
      formMsg:{
        name:"",
        code:"",
        type:"",
        writer:"",
        date:"",
        publisher:"",
        stock:"",
        price:""
      },
      admin_id:"27",
    }
  },
  methods: {
    //3.定义一个init函数，通过设置detailVisible值为true来让弹窗弹出，这个函数会在父组件的方法中被调用
    init(data) {
      this.detailVisible = true;
      this.formMsg.name=data.name;
      this.formMsg.code=data.book_code;
      this.formMsg.type=data.type;
      this.formMsg.writer=data.author;
      this.formMsg.date=data.publish_time;
      this.formMsg.publisher=data.publisher;
      this.formMsg.stock=data.inventory;
      this.formMsg.price=data.price;
      //data是父组件弹窗传递过来的值，我们可以打印看看
      console.log(data);
    },
    handleFileUpload(event){
      const file = event.target.files[0];
      this.fileName = file.name.replace(/\.[^/.]+$/, ''); // 移除文件扩展名
      console.log('文件名',this.fileName)//数据库存的
      this.filePath=require("D:/librarycover/" + this.fileName + ".jpg")
    },
    onSubmit(){
      axios({
        method:"get",
        url:"http://localhost:5000//admin/book/add",
        params:{
          name:this.formMsg.name.toString(),
          code:this.formMsg.code.toString(),
          writer:this.formMsg.writer.toString(),
          date:this.formMsg.date.toString(),
          press:this.formMsg.publisher.toString(),
          stock:this.formMsg.stock.toString(),
          price:this.formMsg.price.toString(),
          picture:this.fileName.toString(),
          type:this.formMsg.type.toString(),
          admin_id:"27"
        }
      }).then((res)=> {
        if(res.data.data ==='书籍新增成功'){
          this.detailVisible = false
          this.$router.go(0)
          this.$message({
            message: res.data.data,
            type:"success"
          })
        }else {
          this.$message({
            message: res.data.data,
            type: "info"
          })

        }
      })
    }
  }
}
</script>

<style scoped>

.dialog{
  width:100%;
  border-radius: 25px;
  padding: 10px;
  margin:auto;
}
.outline{
  padding-left: 5%;
  //display: flex;
  //flex-direction: column;
  width: 450px;
  height: 320px;
}
input.cover{
  display: none;
}
.cover-div{
  width: 140px;
  height: 180px;

  position: relative;
  margin: 8% 5% 0 0;
}
.center{
  position: relative;
  width: 200px;
  height: 150px;
  padding:10px;
  left: 50px;
}

label.cover-label{
  background-color: rgb(237,237,237);
  color: rgb(154,154,154);
  //padding: 100px 20px;
  width: 100%;
  height: 100%;
  border-radius: 5px;
  display: inline-block;
  cursor: pointer;
  text-align: center;
  line-height: 180px;
}
.cancel{
  display: inline-block;
  width: 100px;
  height: 40px;
  margin: 10px;
  position: relative;
  left: 40%;
  background-color: rgb(166,166,166);
  border-radius: 15px;
  border: none;
  color: white;
  cursor: pointer;
}
.cancel:hover{
  background-color: red;
}
.store{
  display: inline-block;
  width: 100px;
  height: 40px;
  margin: 10px;
  position: relative;
  left: 40%;
  background-color: rgb(125,153,191);
  border-radius: 15px;
  border: none;
  color: white;
  cursor: pointer;
}
.store:hover{
  background-color: rgb(85,153,191);
}
.right{
  display: inline-block;
  width: 400px;
  height: 100px;
  position: relative;
  top: -230px;
  right: -320px;
}
div.line{
  width: 340px;
  padding: 10px;
}
div.line>input{
  background-color:rgb(237,237,237) ;
  border-radius: 15px;
  border: none;
  width: 120px;
  float: right;
  height: 25px;
  padding-left: 5px;
}
div.line>label{
  width: 50px;

}
.bookImg-font{
  font-size:15px;
  color:rgba(128, 128, 128, 1);
  cursor: pointer;
}
.inputImg-col{
  height:70%;
  width:30%;
  display: flex;
  justify-content: center;
  align-items: center;

}
.inputImg-gray{
   width:85%;
   height:78%;
   background-color:rgba(237, 237, 237, 1);
   border-radius: 15px;
   margin-left:20px;
   display: flex;
   justify-content: center;
   align-items: center;
 }
.label-left{
  position: relative;
  left: 115px;
}
</style>